package org.game.db.dao;

import com.sojoys.artifact.manager.DataSourceManager;
import java.sql.Connection;
import com.sojoys.artifact.build.data.base.BaseDao;
import java.util.ArrayList;
import com.sojoys.artifact.manager.JsonManager;
import java.sql.SQLException;
import com.google.common.collect.Lists;
import org.apache.commons.dbutils.handlers.MapHandler;
import com.sojoys.artifact.tools.SK_Plus;
import java.sql.ResultSet;
import java.util.Map;
import javax.sql.DataSource;
import org.apache.commons.dbutils.handlers.MapListHandler;
import com.sojoys.artifact.constant.CreateTableEnum;
import com.sojoys.artifact.constant.DataModelKeyEnum;
import org.apache.commons.dbutils.handlers.AbstractListHandler;
import org.apache.commons.dbutils.QueryRunner;
import com.xiaoleilu.hutool.log.Log;
import java.util.Set;
import com.sojoys.artifact.constant.ArtifactErrorCode;
import com.xiaoleilu.hutool.util.StrUtil;
import com.sojoys.artifact.tools.ToolError;
import org.apache.commons.dbutils.DbUtils;
import com.xiaoleilu.hutool.log.LogFactory;
import java.util.List;
import static org.game.db.StaticResource.CurrentScriptRecordResource.*;
import com.alibaba.fastjson.JSONObject;
import org.game.db.bean.CurrentScriptRecord;
import org.apache.commons.dbutils.ResultSetHandler;
import com.google.common.base.Joiner;
import static org.game.db.StaticResource.CurrentScriptRecordResource.*;
/**
 * 当前剧本记录
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
public class CurrentScriptRecordDaoImpl implements BaseDao<Long,CurrentScriptRecord>{
	static Log log = LogFactory.get(CurrentScriptRecordDaoImpl.class);
	
	// 代理
	
	/**
	 * 私有化无参构造方法
	 */
	private CurrentScriptRecordDaoImpl() {}
	  
    private static final CurrentScriptRecordDaoImpl INSTANCE = new CurrentScriptRecordDaoImpl();  
    
    public static CurrentScriptRecordDaoImpl me() {  
        return INSTANCE;  
    }  
	
	public CurrentScriptRecord insert(CurrentScriptRecord currentScriptRecord){
		Connection conn = DataSourceManager.me().getMainConnection();
		return insert(currentScriptRecord,conn);
	}
	
	public CurrentScriptRecord insert(CurrentScriptRecord currentScriptRecord,Connection conn){
		return insert(currentScriptRecord,conn,TABLE_NAME);
	}
	
	public CurrentScriptRecord insert(CurrentScriptRecord currentScriptRecord,DataSource ds){
		try {
			Connection conn = ds.getConnection();
			return insert(currentScriptRecord,conn);
		} catch (Exception e) {
			log.error(e);
			return null;
		}
	}
	
	public CurrentScriptRecord insert(CurrentScriptRecord currentScriptRecord,String tableName){
		Connection conn = DataSourceManager.me().getMainConnection();
		return insert(currentScriptRecord,conn,tableName);
	}
	
	public CurrentScriptRecord insert(CurrentScriptRecord currentScriptRecord,Connection conn,String tableName){
		
		QueryRunner run = new QueryRunner();
		String sql = "INSERT INTO " +tableName+ " (id,create_date,modified_date,reward,dialogue_count,dating_type,score,favor_reward,current_cid,current_script,script_id,selected_node,player_id,role_ids) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
		try {
			int i = run.update(conn,sql,currentScriptRecord.getId(),currentScriptRecord.getCreateDate(),currentScriptRecord.getModifiedDate(),currentScriptRecord.getRewardByte(),currentScriptRecord.getDialogueCountByte(),currentScriptRecord.getDatingType(),currentScriptRecord.getScore(),currentScriptRecord.getFavorReward(),currentScriptRecord.getCurrentCid(),currentScriptRecord.getCurrentScriptByte(),currentScriptRecord.getScriptId(),currentScriptRecord.getSelectedNodeByte(),currentScriptRecord.getPlayerId(),currentScriptRecord.getRoleIdsByte());
			return i > 0 ? currentScriptRecord : null;
		} catch (Exception e) {
			log.error(e);
			return null;
		} finally {
			try{
				DbUtils.close(conn);
			}catch (Exception e1) {
				log.error(e1);
				return null;
			}
		}
	}
	
	public CurrentScriptRecord insert(CurrentScriptRecord currentScriptRecord,DataSource ds,String tableName){
		try {
			Connection conn = ds.getConnection();
			return insert(currentScriptRecord,conn,tableName);
		} catch (Exception e) {
			log.error(e);
			return null;
		}		
	}
	
	public int[] insertBatch(List<CurrentScriptRecord> currentScriptRecords){
		Connection conn = DataSourceManager.me().getMainConnection();
		return insertBatch(currentScriptRecords,conn);
	}
	
	public int[] insertBatch(List<CurrentScriptRecord> currentScriptRecords,Connection conn){
		return insertBatch(currentScriptRecords,conn,TABLE_NAME);
	}
	
	public int[] insertBatch(List<CurrentScriptRecord> currentScriptRecords,DataSource ds){
		try {
			Connection conn = ds.getConnection();
			return insertBatch(currentScriptRecords,conn);
		} catch (Exception e) {
			log.error(e);
			return null;
		}
	}
	
	public int[] insertBatch(List<CurrentScriptRecord> currentScriptRecords,String tableName){
		Connection conn = DataSourceManager.me().getMainConnection();
		return insertBatch(currentScriptRecords,conn,tableName);
	}
	
	public int[] insertBatch(List<CurrentScriptRecord> currentScriptRecords,Connection conn,String tableName){
		QueryRunner run = new QueryRunner();
		String sql = "INSERT INTO " +tableName+ " (id,create_date,modified_date,reward,dialogue_count,dating_type,score,favor_reward,current_cid,current_script,script_id,selected_node,player_id,role_ids) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
		try {
			int columnSize = 14;
			int size = currentScriptRecords.size();
			Object[][] params = new Object[size][columnSize];
			for (int i = 0; i < size; i++) {
				params[i][0] =currentScriptRecords.get(i).getId();
				params[i][1] =currentScriptRecords.get(i).getCreateDate();
				params[i][2] =currentScriptRecords.get(i).getModifiedDate();
				params[i][3] =currentScriptRecords.get(i).getRewardByte();
				params[i][4] =currentScriptRecords.get(i).getDialogueCountByte();
				params[i][5] =currentScriptRecords.get(i).getDatingType();
				params[i][6] =currentScriptRecords.get(i).getScore();
				params[i][7] =currentScriptRecords.get(i).getFavorReward();
				params[i][8] =currentScriptRecords.get(i).getCurrentCid();
				params[i][9] =currentScriptRecords.get(i).getCurrentScriptByte();
				params[i][10] =currentScriptRecords.get(i).getScriptId();
				params[i][11] =currentScriptRecords.get(i).getSelectedNodeByte();
				params[i][12] =currentScriptRecords.get(i).getPlayerId();
				params[i][13] =currentScriptRecords.get(i).getRoleIdsByte();
			}
			int[] is = run.batch(conn,sql,params);
			return is.length > 0 ? is : new int[]{};
		} catch (Exception e) {
			log.error(e);
			return new int[]{};
		} finally {
			try{
				DbUtils.close(conn);
			}catch (Exception e1) {
				log.error(e1);
				return null;
			}
		}
	}
	
	public int[] insertBatch(List<CurrentScriptRecord> currentScriptRecords,DataSource ds,String tableName){
		try {
			Connection conn = ds.getConnection();
			return insertBatch(currentScriptRecords,conn,tableName);
		} catch (Exception e) {
			log.error(e);
			return null;
		}		
	}
	
	public CurrentScriptRecord update(CurrentScriptRecord currentScriptRecord){
		Connection conn = DataSourceManager.me().getMainConnection();
		return update(currentScriptRecord,conn);
	}
	
	public CurrentScriptRecord update(CurrentScriptRecord currentScriptRecord,Connection conn){
		return update(currentScriptRecord,conn,TABLE_NAME);
	}
	
	public CurrentScriptRecord update(CurrentScriptRecord currentScriptRecord,DataSource ds){
		try {
			Connection conn = ds.getConnection();
			return update(currentScriptRecord,conn);
		} catch (Exception e) {
			log.error(e);
			return null;
		}
	}
	
	public CurrentScriptRecord update(CurrentScriptRecord currentScriptRecord,String tableName){
		Connection conn = DataSourceManager.me().getMainConnection();
		return update(currentScriptRecord,conn,tableName);
	}
	
	public CurrentScriptRecord update(CurrentScriptRecord currentScriptRecord,Connection conn,String tableName){
		List<CurrentScriptRecord> currentScriptRecords = new ArrayList<CurrentScriptRecord>();
		currentScriptRecords.add(currentScriptRecord);
		updateBatch(currentScriptRecords,conn,tableName);
		return currentScriptRecord;
	}
	
	public CurrentScriptRecord update(CurrentScriptRecord currentScriptRecord,DataSource ds,String tableName){
		try {
			Connection conn = ds.getConnection();
			return update(currentScriptRecord,conn,tableName);
		} catch (Exception e) {
			log.error(e);
			return null;
		}
	}
	
	public int[] updateBatch(List<CurrentScriptRecord> currentScriptRecords){
		Connection conn = DataSourceManager.me().getMainConnection();
		return updateBatch(currentScriptRecords,conn);
	}
	
	public int[] updateBatch(List<CurrentScriptRecord> currentScriptRecords,Connection conn){
		return updateBatch(currentScriptRecords,conn,TABLE_NAME);
	}
	
	public int[] updateBatch(List<CurrentScriptRecord> currentScriptRecords,DataSource ds){
		try {
			Connection conn = ds.getConnection();
			return updateBatch(currentScriptRecords,conn);
		} catch (Exception e) {
			log.error(e);
			return null;
		}
	}
	
	public int[] updateBatch(List<CurrentScriptRecord> currentScriptRecords,String tableName){
		Connection conn = DataSourceManager.me().getMainConnection();
		return updateBatch(currentScriptRecords,conn,tableName);
	}
	
	public int[] updateBatch(List<CurrentScriptRecord> currentScriptRecords,Connection conn,String tableName){
		QueryRunner run = new QueryRunner();
		String sql = "UPDATE " +tableName+ " SET id = ?,create_date = ?,modified_date = ?,reward = ?,dialogue_count = ?,dating_type = ?,score = ?,favor_reward = ?,current_cid = ?,current_script = ?,script_id = ?,selected_node = ?,player_id = ?,role_ids = ? WHERE id = ?";
		try {
			int columnSize = 14;
			int size = currentScriptRecords.size();
			Object[][] params = new Object[size][columnSize + 1];
			for (int i = 0; i < size; i++) {
				params[i][0] =currentScriptRecords.get(i).getId();
				params[i][1] =currentScriptRecords.get(i).getCreateDate();
				params[i][2] =currentScriptRecords.get(i).getModifiedDate();
				params[i][3] =currentScriptRecords.get(i).getRewardByte();
				params[i][4] =currentScriptRecords.get(i).getDialogueCountByte();
				params[i][5] =currentScriptRecords.get(i).getDatingType();
				params[i][6] =currentScriptRecords.get(i).getScore();
				params[i][7] =currentScriptRecords.get(i).getFavorReward();
				params[i][8] =currentScriptRecords.get(i).getCurrentCid();
				params[i][9] =currentScriptRecords.get(i).getCurrentScriptByte();
				params[i][10] =currentScriptRecords.get(i).getScriptId();
				params[i][11] =currentScriptRecords.get(i).getSelectedNodeByte();
				params[i][12] =currentScriptRecords.get(i).getPlayerId();
				params[i][13] =currentScriptRecords.get(i).getRoleIdsByte();
				params[i][columnSize] =currentScriptRecords.get(i).getId();
			}
			int[] is = run.batch(conn,sql,params);
			return is.length > 0 ? is : new int[]{};
		} catch (Exception e) {
			log.error(e);
			return new int[]{};
		} finally {
			try{
				DbUtils.close(conn);
			}catch (Exception e1) {
				log.error(e1);
				return null;
			}
		}
	}
	
	public int[] updateBatch(List<CurrentScriptRecord> currentScriptRecords,DataSource ds,String tableName){
		try {
			Connection conn = ds.getConnection();
			return updateBatch(currentScriptRecords,conn,tableName);
		} catch (Exception e) {
			log.error(e);
			return null;
		}		
	}
	
	public boolean delete(CurrentScriptRecord currentScriptRecord){
		Connection conn = DataSourceManager.me().getMainConnection();
		return delete(currentScriptRecord,conn);
	}
	
	public boolean delete(CurrentScriptRecord currentScriptRecord,Connection conn){
		return delete(currentScriptRecord,conn,TABLE_NAME);
	}
	
	public boolean delete(CurrentScriptRecord currentScriptRecord,DataSource ds){
		try {
			Connection conn = ds.getConnection();
			return delete(currentScriptRecord,conn);
		} catch (Exception e) {
			log.error(e);
			return false;
		}
	}
	
	public boolean delete(CurrentScriptRecord currentScriptRecord,String tableName){
		Connection conn = DataSourceManager.me().getMainConnection();
		return delete(currentScriptRecord,conn,tableName);
	}
	
	public boolean delete(CurrentScriptRecord currentScriptRecord,Connection conn,String tableName){
		QueryRunner run = new QueryRunner();
		String sql = "DELETE FROM " + tableName + " WHERE id = ?";
		try {
			int i = run.update(conn,sql, currentScriptRecord.getId());
			return i > 0 ? true : false;
		} catch (Exception e) {
			log.error(e);
			return false;
		}finally {
			try{
				DbUtils.close(conn);
			}catch (Exception e1) {
				log.error(e1);
				return false;
			}
		}
	}
	
	public boolean delete(CurrentScriptRecord currentScriptRecord,DataSource ds,String tableName){
		try {
			Connection conn = ds.getConnection();
			return delete(currentScriptRecord,conn,tableName);
		} catch (Exception e) {
			log.error(e);
			return false;
		}
	}
	
	
	public boolean deleteBatch(List<CurrentScriptRecord> currentScriptRecords){
		Connection conn = DataSourceManager.me().getMainConnection();
		return deleteBatch(currentScriptRecords,conn);
	}
	
	public boolean deleteBatch(List<CurrentScriptRecord> currentScriptRecords,Connection conn){
		return deleteBatch(currentScriptRecords,conn,TABLE_NAME);
	}
	
	public boolean deleteBatch(List<CurrentScriptRecord> currentScriptRecords,DataSource ds){
		try {
			Connection conn = ds.getConnection();
			return deleteBatch(currentScriptRecords,conn);
		} catch (Exception e) {
			log.error(e);
			return false;
		}
	}
	
	public boolean deleteBatch(List<CurrentScriptRecord> currentScriptRecords,String tableName){
		Connection conn = DataSourceManager.me().getMainConnection();
		return deleteBatch(currentScriptRecords,conn,tableName);
	}
	
	public boolean deleteBatch(List<CurrentScriptRecord> currentScriptRecords,Connection conn,String tableName){
		QueryRunner run = new QueryRunner();
		String sql = "DELETE FROM " + tableName + " WHERE id = ?";
		try {
			int size = currentScriptRecords.size();
			Object[][] params = new Object[size][1];
			for (int i = 0; i < size; i++) {
				params[i][0] = currentScriptRecords.get(i).getId();
			}
			int[] is = run.batch(conn,sql,params);
			return is.length > 0;
		} catch (Exception e) {
			log.error(e);
			return false;
		}finally {
			try{
				DbUtils.close(conn);
			}catch (Exception e1) {
				log.error(e1);
				return false;
			}
		}
	}
	
	public boolean deleteBatch(List<CurrentScriptRecord> currentScriptRecords,DataSource ds,String tableName){
		try {
			Connection conn = ds.getConnection();
			return deleteBatch(currentScriptRecords,conn,tableName);
		} catch (Exception e) {
			log.error(e);
			return false;
		}
	}

	/**
	 * 根据( id ) 查询
	 */
	public CurrentScriptRecord getById(long id){
		Connection conn = DataSourceManager.me().getMainConnection();
		return getById(id, conn);
	}
	
	public CurrentScriptRecord getById(long id,String tableName){
		Connection conn = DataSourceManager.me().getMainConnection();
		return getById(id, conn,tableName);
	}
	public List<CurrentScriptRecord> getIn(Set<Long> ids){
		Connection conn = DataSourceManager.me().getMainConnection();
		return getIn(ids, conn);
	}
	
	public List<CurrentScriptRecord> getIn(Set<Long> ids,String tableName){
		Connection conn = DataSourceManager.me().getMainConnection();
		return getIn(ids, conn,tableName);
	}
	/**
	 * 根据( player_id ) 查询
	 */
	public List<CurrentScriptRecord> getByPlayerId(int playerId){
		Connection conn = DataSourceManager.me().getMainConnection();
		return getByPlayerId(playerId, conn);
	}
	
	public List<CurrentScriptRecord> getByPlayerId(int playerId,String tableName){
		Connection conn = DataSourceManager.me().getMainConnection();
		return getByPlayerId(playerId, conn,tableName);
	}
	
	public List<CurrentScriptRecord> getByPagePlayerId(int playerId,int page,int pageSize){
		Connection conn = DataSourceManager.me().getMainConnection();
		return getByPagePlayerId(playerId, conn,page,pageSize);
	}
	
	public List<CurrentScriptRecord> getByPagePlayerId(int playerId,String tableName,int page,int pageSize){
		Connection conn = DataSourceManager.me().getMainConnection();
		return getByPagePlayerId(playerId, conn,tableName,page,pageSize);
	}
	
	//Connection
	/**
	 * 根据( id ) 查询
	 */
	public CurrentScriptRecord getById(long id,Connection conn){
		return getById(id,conn,TABLE_NAME);
	}
	
	public CurrentScriptRecord getById(long id,Connection conn,String tableName){
		QueryRunner run = new QueryRunner();
		String sql = "SELECT id,create_date,modified_date,reward,dialogue_count,dating_type,score,favor_reward,current_cid,current_script,script_id,selected_node,player_id,role_ids FROM " + tableName + " WHERE " + "id = ? ORDER BY id ASC LIMIT 1";
		CurrentScriptRecord currentScriptRecord = null; 
		try {
			currentScriptRecord = run.query(conn,sql, new CurrentScriptRecordHandler(), id);
		} catch (Exception e) {
			log.error(e);
		}finally {
			try{
				DbUtils.close(conn);
			}catch (Exception e1) {
				log.error(e1);
				return null;
			}
		}
		return currentScriptRecord;
	}
	public List<CurrentScriptRecord> getIn(Set<Long> ids,Connection conn){
		return getIn(ids,conn,TABLE_NAME);
	}
	
	public List<CurrentScriptRecord> getIn(Set<Long> ids,Connection conn,String tableName){
		List<CurrentScriptRecord> currentScriptRecords = Lists.newArrayList();
		if(ids.isEmpty()) return currentScriptRecords;
		QueryRunner run = new QueryRunner();
		String sql = "SELECT id,create_date,modified_date,reward,dialogue_count,dating_type,score,favor_reward,current_cid,current_script,script_id,selected_node,player_id,role_ids FROM " + tableName + " WHERE " + "id in (%s) ORDER BY id ASC";
		sql = String.format(sql, Joiner.on(",").join(ids));
		try {
			currentScriptRecords = run.query(conn,sql, new CurrentScriptRecordListHandler());
		} catch (Exception e) {
			log.error(e);
		}finally {
			try{
				DbUtils.close(conn);
			}catch (Exception e1) {
				log.error(e1);
				return null;
			}
		}
		return currentScriptRecords;
	}
	/**
	 * 根据( player_id ) 查询
	 */
	public List<CurrentScriptRecord> getByPlayerId(int playerId,Connection conn){
		return getByPlayerId(playerId,conn,TABLE_NAME);
	}
	
	public List<CurrentScriptRecord> getByPlayerId(int playerId,Connection conn,String tableName){
		QueryRunner run = new QueryRunner();
		String sql = "SELECT id,create_date,modified_date,reward,dialogue_count,dating_type,score,favor_reward,current_cid,current_script,script_id,selected_node,player_id,role_ids FROM " + tableName + " WHERE " + "player_id = ? ORDER BY id ASC";
		List<CurrentScriptRecord> currentScriptRecords = Lists.newArrayList();
		try {
			currentScriptRecords = run.query(conn, sql, new CurrentScriptRecordListHandler(), playerId);
		} catch (Exception e) {
			log.error(e);
		}finally {
			try{
				DbUtils.close(conn);
			}catch (Exception e1) {
				log.error(e1);
				return null;
			}
		}
		return currentScriptRecords;
	}
	
	//-----------------------------------page-----------------------------------
	public List<CurrentScriptRecord> getByPagePlayerId(int playerId,Connection conn,int page,int pageSize){
		return getByPagePlayerId(playerId,conn,TABLE_NAME,page,pageSize);
	}
	
	public List<CurrentScriptRecord> getByPagePlayerId(int playerId,Connection conn,String tableName,int page,int pageSize){
		QueryRunner run = new QueryRunner();
		page = ((page-1) * pageSize);
		String sql = "SELECT id,create_date,modified_date,reward,dialogue_count,dating_type,score,favor_reward,current_cid,current_script,script_id,selected_node,player_id,role_ids FROM " + tableName + " WHERE " + "player_id = ? ORDER BY id ASC LIMIT " + page + " , " +pageSize;
		List<CurrentScriptRecord> currentScriptRecords = Lists.newArrayList();
		try {
			currentScriptRecords = run.query(conn, sql, new CurrentScriptRecordListHandler(), playerId);
		} catch (Exception e) {
			log.error(e);
		}finally {
			try{
				DbUtils.close(conn);
			}catch (Exception e1) {
				log.error(e1);
				return null;
			}
		}
		return currentScriptRecords;
	}
	
	//DataSource
	/**
	 * 根据( id ) 查询
	 */
	public CurrentScriptRecord getById(long id,DataSource ds){
		try {
			Connection conn = ds.getConnection();
			return getById(id, conn);
		} catch (Exception e) {
			log.error(e);
			return null;
		}
	}
	
	public CurrentScriptRecord getById(long id,DataSource ds,String tableName){
		try {
			Connection conn = ds.getConnection();
			return getById(id, conn, tableName);
		} catch (Exception e) {
			log.error(e);
			return null;
		}
	}
	public List<CurrentScriptRecord> getIn(Set<Long> ids,DataSource ds){
		try {
			Connection conn = ds.getConnection();
			return getIn(ids, conn);
		} catch (Exception e) {
			log.error(e);
			return null;
		}
	}
	
	public List<CurrentScriptRecord> getIn(Set<Long> ids,DataSource ds,String tableName){
		try {
			Connection conn = ds.getConnection();
			return getIn(ids, conn, tableName);
		} catch (Exception e) {
			log.error(e);
			return null;
		}
	}
	/**
	 * 根据( player_id ) 查询
	 */
	public List<CurrentScriptRecord> getByPlayerId(int playerId,DataSource ds){
		try {
			Connection conn = ds.getConnection();
			return getByPlayerId(playerId, conn);
		} catch (Exception e) {
			log.error(e);
			return null;
		}
	}
	
	public List<CurrentScriptRecord> getByPlayerId(int playerId,DataSource ds,String tableName){
		try {
			Connection conn = ds.getConnection();
			return getByPlayerId(playerId, conn, tableName);
		} catch (Exception e) {
			log.error(e);
			return null;
		}
	}
	//-----------------------------------page-----------------------------------
	public List<CurrentScriptRecord> getByPagePlayerId(int playerId,DataSource ds,int page,int pageSize){
		try {
			Connection conn = ds.getConnection();
			return getByPagePlayerId(playerId, conn,page,pageSize);
		} catch (Exception e) {
			log.error(e);
			return null;
		}
	}
	
	public List<CurrentScriptRecord> getByPagePlayerId(int playerId,DataSource ds,String tableName,int page,int pageSize){
		try {
			Connection conn = ds.getConnection();
			return getByPagePlayerId(playerId, conn,page,pageSize);
		} catch (Exception e) {
			log.error(e);
			return null;
		}
	}
	
	
	public List<CurrentScriptRecord> getAll(){
		Connection conn = DataSourceManager.me().getMainConnection();
		return getAll(conn);
	}
	
	public List<CurrentScriptRecord> getAll(Connection conn){
		return getAll(conn,TABLE_NAME);
	}
	
	public List<CurrentScriptRecord> getAll(DataSource ds){
		try {
			Connection conn = ds.getConnection();
			return getAll(conn);
		} catch (Exception e) {
			log.error(e);
			return null;
		}
	}
	
	public List<CurrentScriptRecord> getAll(String tableName){
		Connection conn = DataSourceManager.me().getMainConnection();
		return getAll(conn,tableName);
	}
	
	public List<CurrentScriptRecord> getAll(Connection conn,String tableName){
		QueryRunner run = new QueryRunner();
		String sql = "SELECT id,create_date,modified_date,reward,dialogue_count,dating_type,score,favor_reward,current_cid,current_script,script_id,selected_node,player_id,role_ids FROM " + tableName + " ORDER BY id ASC";
		List<CurrentScriptRecord> currentScriptRecords = Lists.newArrayList();
		try {
			currentScriptRecords = run.query(conn, sql, new CurrentScriptRecordListHandler());
		} catch (Exception e) {
			log.error(e);
		}finally {
			try{
				DbUtils.close(conn);
			}catch (Exception e1) {
				log.error(e1);
				return null;
			}
		}
		return currentScriptRecords;
	}
	
	public List<CurrentScriptRecord> getAll(DataSource ds,String tableName){
		try {
			Connection conn = ds.getConnection();
			return getAll(conn,tableName);
		} catch (Exception e) {
			log.error(e);
			return null;
		}		
	}
	
	public List<CurrentScriptRecord> getAllPage(int page,int pageSize){
		Connection conn = DataSourceManager.me().getMainConnection();
		return getAllPage(conn,page,pageSize);
	}
	
	public List<CurrentScriptRecord> getAllPage(Connection conn,int page,int pageSize){
		return getAllPage(conn,TABLE_NAME,page,pageSize);
	}
	
	public List<CurrentScriptRecord> getAllPage(DataSource ds,int page,int pageSize){
		try {
			Connection conn = ds.getConnection();
			return getAllPage(conn,page,pageSize);
		} catch (Exception e) {
			log.error(e);
			return null;
		}
	}
	
	public List<CurrentScriptRecord> getAllPage(String tableName,int page,int pageSize){
		Connection conn = DataSourceManager.me().getMainConnection();
		return getAllPage(conn,tableName,page,pageSize);
	}
	
	public List<CurrentScriptRecord> getAllPage(Connection conn,String tableName,int page,int pageSize){
		QueryRunner run = new QueryRunner();
		page = ((page-1) * pageSize);
		String sql = "SELECT id,create_date,modified_date,reward,dialogue_count,dating_type,score,favor_reward,current_cid,current_script,script_id,selected_node,player_id,role_ids FROM " + tableName + " ORDER BY id ASC LIMIT " + page + " , " +pageSize;
		List<CurrentScriptRecord> currentScriptRecords = Lists.newArrayList();
		try {
			currentScriptRecords = run.query(conn, sql, new CurrentScriptRecordListHandler());
		} catch (Exception e) {
			log.error(e);
		}finally {
			try{
				DbUtils.close(conn);
			}catch (Exception e1) {
				log.error(e1);
				return null;
			}
		}
		return currentScriptRecords;
	}
	
	public List<CurrentScriptRecord> getAllPage(DataSource ds,String tableName,int page,int pageSize){
		try {
			Connection conn = ds.getConnection();
			return getAllPage(conn,tableName,page,pageSize);
		} catch (Exception e) {
			log.error(e);
			return null;
		}		
	}
	
	public boolean truncate(){
		Connection conn = DataSourceManager.me().getMainConnection();
		return truncate(conn);
	}
	
	public boolean truncate(Connection conn){
		return truncate(conn,TABLE_NAME);
	}
	
	public boolean truncate(DataSource ds){
		try {
			Connection conn = ds.getConnection();
			return truncate(conn);
		} catch (Exception e) {
			log.error(e);
			return false;
		}
	}
	
	public boolean truncate(String tableName){
		Connection conn = DataSourceManager.me().getMainConnection();
		return truncate(conn,tableName);
	}
	
	public boolean truncate(Connection conn,String tableName){
		QueryRunner run = new QueryRunner();
		String sql = "TRUNCATE " + tableName;
		try {
			run.update(conn, sql);
			return true;
		} catch (Exception e) {
			log.error(e);
			return false;
		} finally {
			try {
				DbUtils.close(conn);
			} catch (Exception e1) {
				log.error(e1);
				return false;
			}
		}
	}
	
	public boolean truncate(DataSource ds,String tableName){
		try {
			Connection conn = ds.getConnection();
			return truncate(conn,tableName);
		} catch (Exception e) {
			log.error(e);
			return false;
		}
	}
	
	// 执行自定义查询sql
	public List<Map<String,Object>> executeQuerySql(String sql){
		Connection conn = DataSourceManager.me().getMainConnection();
		return executeQuerySql(conn,sql);
	}
	
	public List<Map<String,Object>> executeQuerySql(Connection conn,String sql){
		return executeQuerySql(conn,TABLE_NAME,sql);
	}
	
	public List<Map<String,Object>> executeQuerySql(DataSource ds,String sql){
		try {
			Connection conn = ds.getConnection();
			return executeQuerySql(conn,sql);
		} catch (Exception e) {
			log.error(e);
			return null;
		}
	}
	
	public List<Map<String,Object>> executeQuerySql(String tableName,String sql){
		Connection conn = DataSourceManager.me().getMainConnection();
		return executeQuerySql(conn,tableName);
	}
	
	public List<Map<String,Object>> executeQuerySql(Connection conn,String tableName,String sql){
		QueryRunner run = new QueryRunner();
		ToolError.isAndTrue(ArtifactErrorCode.SQL_PERMISSIONS_ERROR, !StrUtil.startWithIgnoreCase(sql, "SELECT"));
		List<Map<String,Object>> list = Lists.newArrayList();
		try {
			list = run.query(conn,sql, new MapListHandler());
		} catch (Exception e) {
			log.error(e);
		}finally {
			try{
				DbUtils.close(conn);
			}catch (Exception e1) {
				log.error(e1);
				return null;
			}
		}
		return list;
	}
	/**
	public List<Map<String,Object>> executeQuerySql(DataSource ds,String tableName){
		try {
			Connection conn = ds.getConnection();
			return executeQuerySql(conn,tableName);
		} catch (Exception e) {
			log.error(e);
			return null;
		}
	}
	*/
	
	//
	
	//
	
	//Drop Table
	public boolean drop(){
		Connection conn = DataSourceManager.me().getMainConnection();
		return drop(conn);
	}
	
	public boolean drop(Connection conn){
		return drop(conn,TABLE_NAME);
	}
	
	public boolean drop(DataSource ds){
		try {
			Connection conn = ds.getConnection();
			return drop(conn);
		} catch (Exception e) {
			log.error(e);
			return false;
		}
	}
	
	public boolean drop(String tableName){
		Connection conn = DataSourceManager.me().getMainConnection();
		return drop(conn,tableName);
	}
	
	public boolean drop(Connection conn,String tableName){
		QueryRunner run = new QueryRunner();
		String sql = "DROP TABLE " + tableName;
		try {
			run.update(conn, sql);
			return true;
		} catch (Exception e) {
			log.error(e);
			return false;
		} finally {
			try {
				DbUtils.close(conn);
			} catch (Exception e1) {
				log.error(e1);
				return false;
			}
		}
	}
	
	public boolean drop(DataSource ds,String tableName){
		try {
			Connection conn = ds.getConnection();
			return drop(conn,tableName);
		} catch (Exception e) {
			log.error(e);
			return false;
		}
	}
	
	
	//create
	public boolean createTable(CreateTableEnum type){
		Connection conn = DataSourceManager.me().getMainConnection();
		return createTable(conn,type);
	}
	
	public boolean createTable(Connection conn,CreateTableEnum type){
		return createTable(conn,TABLE_NAME,type);
	}
	
	public boolean createTable(DataSource ds,CreateTableEnum type){
		try {
			Connection conn = ds.getConnection();
			return createTable(conn,type);
		} catch (Exception e) {
			log.error(e);
			return false;
		}
	}
	
	public boolean createTable(String tableName,CreateTableEnum type){
		Connection conn = DataSourceManager.me().getMainConnection();
		return createTable(conn,tableName,type);
	}
	
	public boolean createTable(Connection conn,String tableName,CreateTableEnum type){
		QueryRunner run = new QueryRunner();
		SK_Plus plus = SK_Plus.b("CREATE TABLE IF NOT EXISTS `", tableName,"` (");
		plus.a("  `id` BIGINT(20) NOT NULL  COMMENT '',");	
		plus.a("  `create_date` DATETIME   COMMENT '',");
		plus.a("  `modified_date` DATETIME   COMMENT '',");
		plus.a("  `reward` BLOB(65535)   COMMENT 'remarks=本次剧本已经获得的奖励',");	
		plus.a("  `dialogue_count` BLOB(65535)   COMMENT 'remarks=对话次数记录',");	
		plus.a("  `dating_type` INT(11)   COMMENT 'remarks=约会类型',");	
		plus.a("  `score` INT(11)   COMMENT 'remarks=积分',");	
		plus.a("  `favor_reward` INT(11)   COMMENT 'remarks=好感度奖励',");	
		plus.a("  `current_cid` INT(11)   COMMENT 'remarks=当前节点cid',");	
		plus.a("  `current_script` BLOB(65535)   COMMENT 'remarks=剧本分支节点',");	
		plus.a("  `script_id` INT(11)   COMMENT 'remarks=剧本id',");	
		plus.a("  `selected_node` BLOB(65535)   COMMENT 'remarks=被选择的节点',");	
		plus.a("  `player_id` INT(11) NOT NULL  COMMENT 'remarks=玩家id',");	
		plus.a("  `role_ids` BLOB(65535)   COMMENT 'remarks=看板娘id',");	
		plus.a("  PRIMARY KEY (`id`),");
		if (CreateTableEnum.LOGIC.equals(type) || CreateTableEnum.DESIGN.equals(type)) {
			plus.a("  KEY `fk_current_script_record_player1_idx` (`player_id`),");
			
		}
		if (CreateTableEnum.DESIGN.equals(type)) {
			plus.a("  CONSTRAINT `fk_current_script_record_player1` FOREIGN KEY (`player_id`) REFERENCES `player` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,");
		}
		String sql = plus.e();
		sql = StrUtil.subPre(sql, sql.lastIndexOf(StrUtil.C_COMMA));
		sql+=") ENGINE=InnoDB DEFAULT CHARSET=utf8  COMMENT='remarks=当前剧本记录';";
		try {
			run.update(conn, sql);
			return true;
		} catch (Exception e) {
			log.error(e);
			return false;
		} finally {
			try {
				DbUtils.close(conn);
			} catch (Exception e1) {
				log.error(e1);
				return false;
			}
		}
	}
	
	public boolean createTable(DataSource ds,String tableName,CreateTableEnum type){
		try {
			Connection conn = ds.getConnection();
			return createTable(conn,tableName,type);
		} catch (Exception e) {
			log.error(e);
			return false;
		}
	}
	
	
	public String getTableName(){
		return TABLE_NAME;
	}
	
	@SuppressWarnings("unchecked")
	public List<Map<String,Object>> getColumnsInfo(){
		SK_Plus sp = SK_Plus.newSK_Plus();
		sp.a("[");
       	sp.a("{\"TABLE_CAT\":\"dal_game_design\",\"BUFFER_LENGTH\":65535,\"IS_NULLABLE\":\"NO\",\"TABLE_NAME\":\"current_script_record\",\"COLUMN_NAME\":\"id\",\"NULLABLE\":0,\"REMARKS\":\"\",\"DECIMAL_DIGITS\":0,\"NUM_PREC_RADIX\":10,\"SQL_DATETIME_SUB\":0,\"IS_GENERATEDCOLUMN\":\"NO\",\"IS_AUTOINCREMENT\":\"NO\",\"SQL_DATA_TYPE\":0,\"ORDINAL_POSITION\":1,\"DATA_TYPE\":-5,\"TYPE_NAME\":\"BIGINT\",\"COLUMN_SIZE\":20}").a(",");
       	sp.a("{\"TABLE_CAT\":\"dal_game_design\",\"BUFFER_LENGTH\":65535,\"IS_NULLABLE\":\"YES\",\"TABLE_NAME\":\"current_script_record\",\"COLUMN_NAME\":\"create_date\",\"NULLABLE\":1,\"REMARKS\":\"\",\"NUM_PREC_RADIX\":10,\"SQL_DATETIME_SUB\":0,\"IS_GENERATEDCOLUMN\":\"NO\",\"IS_AUTOINCREMENT\":\"NO\",\"SQL_DATA_TYPE\":0,\"ORDINAL_POSITION\":2,\"DATA_TYPE\":93,\"TYPE_NAME\":\"DATETIME\",\"COLUMN_SIZE\":19}").a(",");
       	sp.a("{\"TABLE_CAT\":\"dal_game_design\",\"BUFFER_LENGTH\":65535,\"IS_NULLABLE\":\"YES\",\"TABLE_NAME\":\"current_script_record\",\"COLUMN_NAME\":\"modified_date\",\"NULLABLE\":1,\"REMARKS\":\"\",\"NUM_PREC_RADIX\":10,\"SQL_DATETIME_SUB\":0,\"IS_GENERATEDCOLUMN\":\"NO\",\"IS_AUTOINCREMENT\":\"NO\",\"SQL_DATA_TYPE\":0,\"ORDINAL_POSITION\":3,\"DATA_TYPE\":93,\"TYPE_NAME\":\"DATETIME\",\"COLUMN_SIZE\":19}").a(",");
       	sp.a("{\"TABLE_CAT\":\"dal_game_design\",\"BUFFER_LENGTH\":65535,\"IS_NULLABLE\":\"YES\",\"TABLE_NAME\":\"current_script_record\",\"COLUMN_NAME\":\"reward\",\"NULLABLE\":1,\"REMARKS\":\"remarks=本次剧本已经获得的奖励\ntype=Map\",\"NUM_PREC_RADIX\":10,\"SQL_DATETIME_SUB\":0,\"IS_GENERATEDCOLUMN\":\"NO\",\"IS_AUTOINCREMENT\":\"NO\",\"SQL_DATA_TYPE\":0,\"CHAR_OCTET_LENGTH\":65535,\"ORDINAL_POSITION\":4,\"DATA_TYPE\":-4,\"TYPE_NAME\":\"BLOB\",\"COLUMN_SIZE\":65535}").a(",");
       	sp.a("{\"TABLE_CAT\":\"dal_game_design\",\"BUFFER_LENGTH\":65535,\"IS_NULLABLE\":\"YES\",\"TABLE_NAME\":\"current_script_record\",\"COLUMN_NAME\":\"dialogue_count\",\"NULLABLE\":1,\"REMARKS\":\"remarks=对话次数记录\ntype=Map\",\"NUM_PREC_RADIX\":10,\"SQL_DATETIME_SUB\":0,\"IS_GENERATEDCOLUMN\":\"NO\",\"IS_AUTOINCREMENT\":\"NO\",\"SQL_DATA_TYPE\":0,\"CHAR_OCTET_LENGTH\":65535,\"ORDINAL_POSITION\":5,\"DATA_TYPE\":-4,\"TYPE_NAME\":\"BLOB\",\"COLUMN_SIZE\":65535}").a(",");
       	sp.a("{\"TABLE_CAT\":\"dal_game_design\",\"BUFFER_LENGTH\":65535,\"IS_NULLABLE\":\"YES\",\"TABLE_NAME\":\"current_script_record\",\"COLUMN_NAME\":\"dating_type\",\"NULLABLE\":1,\"REMARKS\":\"remarks=约会类型\",\"DECIMAL_DIGITS\":0,\"NUM_PREC_RADIX\":10,\"SQL_DATETIME_SUB\":0,\"IS_GENERATEDCOLUMN\":\"NO\",\"IS_AUTOINCREMENT\":\"NO\",\"SQL_DATA_TYPE\":0,\"ORDINAL_POSITION\":6,\"DATA_TYPE\":4,\"TYPE_NAME\":\"INT\",\"COLUMN_SIZE\":11}").a(",");
       	sp.a("{\"TABLE_CAT\":\"dal_game_design\",\"BUFFER_LENGTH\":65535,\"IS_NULLABLE\":\"YES\",\"TABLE_NAME\":\"current_script_record\",\"COLUMN_NAME\":\"score\",\"NULLABLE\":1,\"REMARKS\":\"remarks=积分\",\"DECIMAL_DIGITS\":0,\"NUM_PREC_RADIX\":10,\"SQL_DATETIME_SUB\":0,\"IS_GENERATEDCOLUMN\":\"NO\",\"IS_AUTOINCREMENT\":\"NO\",\"SQL_DATA_TYPE\":0,\"ORDINAL_POSITION\":7,\"DATA_TYPE\":4,\"TYPE_NAME\":\"INT\",\"COLUMN_SIZE\":11}").a(",");
       	sp.a("{\"TABLE_CAT\":\"dal_game_design\",\"BUFFER_LENGTH\":65535,\"IS_NULLABLE\":\"YES\",\"TABLE_NAME\":\"current_script_record\",\"COLUMN_NAME\":\"favor_reward\",\"NULLABLE\":1,\"REMARKS\":\"remarks=好感度奖励\",\"DECIMAL_DIGITS\":0,\"NUM_PREC_RADIX\":10,\"SQL_DATETIME_SUB\":0,\"IS_GENERATEDCOLUMN\":\"NO\",\"IS_AUTOINCREMENT\":\"NO\",\"SQL_DATA_TYPE\":0,\"ORDINAL_POSITION\":8,\"DATA_TYPE\":4,\"TYPE_NAME\":\"INT\",\"COLUMN_SIZE\":11}").a(",");
       	sp.a("{\"TABLE_CAT\":\"dal_game_design\",\"BUFFER_LENGTH\":65535,\"IS_NULLABLE\":\"YES\",\"TABLE_NAME\":\"current_script_record\",\"COLUMN_NAME\":\"current_cid\",\"NULLABLE\":1,\"REMARKS\":\"remarks=当前节点cid\",\"DECIMAL_DIGITS\":0,\"NUM_PREC_RADIX\":10,\"SQL_DATETIME_SUB\":0,\"IS_GENERATEDCOLUMN\":\"NO\",\"IS_AUTOINCREMENT\":\"NO\",\"SQL_DATA_TYPE\":0,\"ORDINAL_POSITION\":9,\"DATA_TYPE\":4,\"TYPE_NAME\":\"INT\",\"COLUMN_SIZE\":11}").a(",");
       	sp.a("{\"TABLE_CAT\":\"dal_game_design\",\"BUFFER_LENGTH\":65535,\"IS_NULLABLE\":\"YES\",\"TABLE_NAME\":\"current_script_record\",\"COLUMN_NAME\":\"current_script\",\"NULLABLE\":1,\"REMARKS\":\"remarks=剧本分支节点\ntype=Map\",\"NUM_PREC_RADIX\":10,\"SQL_DATETIME_SUB\":0,\"IS_GENERATEDCOLUMN\":\"NO\",\"IS_AUTOINCREMENT\":\"NO\",\"SQL_DATA_TYPE\":0,\"CHAR_OCTET_LENGTH\":65535,\"ORDINAL_POSITION\":10,\"DATA_TYPE\":-4,\"TYPE_NAME\":\"BLOB\",\"COLUMN_SIZE\":65535}").a(",");
       	sp.a("{\"TABLE_CAT\":\"dal_game_design\",\"BUFFER_LENGTH\":65535,\"IS_NULLABLE\":\"YES\",\"TABLE_NAME\":\"current_script_record\",\"COLUMN_NAME\":\"script_id\",\"NULLABLE\":1,\"REMARKS\":\"remarks=剧本id\",\"DECIMAL_DIGITS\":0,\"NUM_PREC_RADIX\":10,\"SQL_DATETIME_SUB\":0,\"IS_GENERATEDCOLUMN\":\"NO\",\"IS_AUTOINCREMENT\":\"NO\",\"SQL_DATA_TYPE\":0,\"ORDINAL_POSITION\":11,\"DATA_TYPE\":4,\"TYPE_NAME\":\"INT\",\"COLUMN_SIZE\":11}").a(",");
       	sp.a("{\"TABLE_CAT\":\"dal_game_design\",\"BUFFER_LENGTH\":65535,\"IS_NULLABLE\":\"YES\",\"TABLE_NAME\":\"current_script_record\",\"COLUMN_NAME\":\"selected_node\",\"NULLABLE\":1,\"REMARKS\":\"remarks=被选择的节点\ntype=List\",\"NUM_PREC_RADIX\":10,\"SQL_DATETIME_SUB\":0,\"IS_GENERATEDCOLUMN\":\"NO\",\"IS_AUTOINCREMENT\":\"NO\",\"SQL_DATA_TYPE\":0,\"CHAR_OCTET_LENGTH\":65535,\"ORDINAL_POSITION\":12,\"DATA_TYPE\":-4,\"TYPE_NAME\":\"BLOB\",\"COLUMN_SIZE\":65535}").a(",");
       	sp.a("{\"TABLE_CAT\":\"dal_game_design\",\"BUFFER_LENGTH\":65535,\"IS_NULLABLE\":\"NO\",\"TABLE_NAME\":\"current_script_record\",\"COLUMN_NAME\":\"player_id\",\"NULLABLE\":0,\"REMARKS\":\"remarks=玩家id\",\"DECIMAL_DIGITS\":0,\"NUM_PREC_RADIX\":10,\"SQL_DATETIME_SUB\":0,\"IS_GENERATEDCOLUMN\":\"NO\",\"IS_AUTOINCREMENT\":\"NO\",\"SQL_DATA_TYPE\":0,\"ORDINAL_POSITION\":13,\"DATA_TYPE\":4,\"TYPE_NAME\":\"INT\",\"COLUMN_SIZE\":11}").a(",");
       	sp.a("{\"TABLE_CAT\":\"dal_game_design\",\"BUFFER_LENGTH\":65535,\"IS_NULLABLE\":\"YES\",\"TABLE_NAME\":\"current_script_record\",\"COLUMN_NAME\":\"role_ids\",\"NULLABLE\":1,\"REMARKS\":\"remarks=看板娘id\ntype=List\n\",\"NUM_PREC_RADIX\":10,\"SQL_DATETIME_SUB\":0,\"IS_GENERATEDCOLUMN\":\"NO\",\"IS_AUTOINCREMENT\":\"NO\",\"SQL_DATA_TYPE\":0,\"CHAR_OCTET_LENGTH\":65535,\"ORDINAL_POSITION\":14,\"DATA_TYPE\":-4,\"TYPE_NAME\":\"BLOB\",\"COLUMN_SIZE\":65535}").a(",");
        sp.a("]");
        return JsonManager.me().getJson().parse(sp.e(), List.class);
	}
	
	@SuppressWarnings("unchecked")
	public List<Map<String,Object>> getIndexsInfo(){
		SK_Plus sp = SK_Plus.newSK_Plus();
		sp.a("[");
       	sp.a("{\"NON_UNIQUE\":false,\"TABLE_CAT\":\"dal_game_design\",\"TABLE_NAME\":\"current_script_record\",\"COLUMN_NAME\":\"id\",\"INDEX_NAME\":\"PRIMARY\",\"ASC_OR_DESC\":\"A\",\"INDEX_QUALIFIER\":\"dal_game_design\",\"ORDINAL_POSITION\":1,\"CARDINALITY\":0,\"TYPE\":3}").a(",");
       	sp.a("{\"NON_UNIQUE\":true,\"TABLE_CAT\":\"dal_game_design\",\"TABLE_NAME\":\"current_script_record\",\"COLUMN_NAME\":\"player_id\",\"INDEX_NAME\":\"fk_current_script_record_player1_idx\",\"ASC_OR_DESC\":\"A\",\"INDEX_QUALIFIER\":\"dal_game_design\",\"ORDINAL_POSITION\":1,\"CARDINALITY\":0,\"TYPE\":3}").a(",");
        sp.a("]");
        return JsonManager.me().getJson().parse(sp.e(), List.class);
	}
//自定义内容起始位置
class CurrentScriptRecordHandler implements ResultSetHandler<CurrentScriptRecord>{
	@Override
	public CurrentScriptRecord handle(ResultSet rs) throws SQLException {
		return rs.next() ? CurrentScriptRecord.builder().createForResultSet(rs).syncIndexValues() : null ;
	}
}

class CurrentScriptRecordListHandler extends AbstractListHandler<CurrentScriptRecord>{
	@Override
	protected CurrentScriptRecord handleRow(ResultSet rs) throws SQLException {
		return CurrentScriptRecord.builder().createForResultSet(rs).syncIndexValues();
	}
}

//自定义内容结束位置
}

